package com.jiawang.core.config;

import com.jiawang.core.log.LogUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;

/**
 * @author LiJiaWang
 * 创建时间: 16:03
 * 项目的名字: jio_writer_tomcat
 * <p style="color:#33CCFF">初始配置类</p>
 */
public class ServerConfig {
    private  LogUtils log =new LogUtils(ServerConfig.class.getCanonicalName());

    /**
     * 端口号
     */
    private int serverPort = 8080;

    /**
     * this
     */
    private static ServerConfig config = new ServerConfig();

    private ServerConfig() {
        long start = System.currentTimeMillis();

        log.info("程序开始");
        log.info("读取根目录下的名为application的配置文件");
        InputStream stream = this.getClass().getClassLoader().getResourceAsStream("application.properties");

        Optional<InputStream> streamOptional = Optional.ofNullable(stream);
        streamOptional.ifPresent(item -> {
            log.info("读取成功,准备加载到io对象");
            Properties properties = new Properties();
            try {
                properties.load(item);
                log.info("加载成功--->开始读取对应的配置信息");
                Optional.ofNullable(properties.getProperty("serverPort")).ifPresent(value -> {
                    this.serverPort = Integer.parseInt(value);
                });
            } catch (IOException e) {
                e.printStackTrace();
                log.warning("读取配置文件错误,使用默认配置启动");
            }
        });
        log.info("对应的端口号为" + serverPort);
        long end = System.currentTimeMillis();
        log.info("启动成功消耗时间" + (end - start) + "豪秒");
    }

    public static ServerConfig getInstance() {

        return config;
    }

    public int getServerPort() {
        return serverPort;
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }
}
